package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.Activity;
import com.burgeon.backend.SHOPBACKEND.entity.SevenDaysReturn;
import com.burgeon.backend.SHOPBACKEND.entity.mailActivity.Gabalnara;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendActivityMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackendActivityService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BackendActivityServiceImpl implements BackendActivityService {
    @Autowired
    private BackendActivityMapper backendActivityMapper;

    /**
     *
     * @param type ms pt ys kj
     * @param state
     * @param account
     */
    @Override
    public void sevenDaysReturnSet(String type, String state, String account) {
        backendActivityMapper.SevenDaysReturnSet(type,state,account);
    }

    @Override
    public SevenDaysReturn sevenDaysReturnData(String account) {
        return backendActivityMapper.SevenDaysReturnData(account);
    }

    @Override
    public Gabalnara getExemptionFromPostage(String account) {
        return backendActivityMapper.findMailActivityData(account);
    }

    @Override
    public void exemptionFromPostage(String price, String isUse, String account) throws ErrorDataException {
        //查询 空新增 修改 是否启用
        Gabalnara data = backendActivityMapper.findMailActivityData(account);

        if (null == data) {
            //新增
            data = new Gabalnara();
            data.setPrice(price);
            data.setIsUse(isUse);
            data.setToken(account);
            backendActivityMapper.createMailActivity(data);
        } else {
            //修改
            data = new Gabalnara();
            data.setPrice(price);
            data.setIsUse(isUse);
            data.setToken(account);
            backendActivityMapper.upMailActivity(data);
        }

    }

    @Override
    public List<Activity> getData(String account) {
        List<Activity> data = backendActivityMapper.GetAppActivity(account);
        Activity activity = new Activity();
        activity.setId("0");
        activity.setName("不参加活动");
        activity.setType("0");
        activity.setValue1("0");
        activity.setValue2("0");
        data.add(activity);

        return data;
    }

    @Override
    public void addData(Activity activity) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        activity.setCreateTime(sdf.format(new Date()));
        backendActivityMapper.createActivity(activity);
    }

    @Override
    public void upData(Activity activity) {
        backendActivityMapper.upActivity(activity);
    }

    @Override
    public void delData(String id) {


        backendActivityMapper.delActivity(id);
        //查询活动id商品id
        List<String> ids = backendActivityMapper.findGoodsIdByAid(id);
        //修改活动id为0 值不参加任何活动
        for (String gid : ids) {
            backendActivityMapper.upGoodsActivity(gid, "不参加活动", "0");
        }
    }


    public void IsUse(){
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String newDate = sf.format(date);
        Date d = null;
        try {
            d = sf.parse(newDate);
            System.err.println(d);

            ArrayList<Activity> activities = backendActivityMapper.FindShopActivity();
            for (Activity activity:activities) {
                //判断是否过期
                if(sf.parse(activity.getEndTime()).before(d)){
                    //修改isUse
                    backendActivityMapper.soldOutGoodsByActivityID(activity.getId());
                    backendActivityMapper.upActivityIsUse(activity.getId());
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }



    }


}
